<?php
/**
 * Created by PhpStorm.
 * User: luwei
 * Date: 2019-03-13
 * Time: 15:07
 */

namespace App\Library\String;
use      App\Exceptions\GeneralException;
class UnionLoginTools {
    //public static $key = config('');
    /**
     * 省呗签名
     * @param $data
     * @return string
     * @author luwei
     * @date 2019-03-13 15:11
     */
    public static function ProvincialSign(array $data){
        $key = config('union_login.union_sheng_bai_key');
        $aes = new Aes();
        $data = $aes->encrypt($data);
        $data = md5(md5($data).$key);
        return $data;
    }


    /**
     * 省呗验签
     * @param $data
     * @param $sign
     * @return string
     * @author luwei
     * @date 2019-03-13 15:11
     */
    public static function ProvincialSignCheck(array $data, $sign){
        $signData = self::ProvincialSign($data);
        if($signData == $sign){
            return true;
        }else{
            return false;
        }
    }

    public static function ProvincialSignProCheck($data, $sign){
        $key = config('union_login.union_sheng_bai_key');
        $signData = md5(md5($data).$key);
        if($signData == $sign){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 洋钱罐签名
     * @param $data
     * @return string
     * @author luwei
     * @date 2019-03-13 17:37
     */
    public static function piggyBankSign($data){
        $pri_key_file = dirname(dirname(dirname(__DIR__))) . '/privateKey/' . 'piggy_bank.pem';
        if (!file_exists($pri_key_file)) {
            throw new GeneralException('私钥文件不存在');
        };
        $priKey = file_get_contents($pri_key_file);
        $res = openssl_get_privatekey($priKey);
        $str = http_build_query($data);
        openssl_sign($str, $sign, $res, OPENSSL_ALGO_MD5);
        openssl_free_key($res);
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * 洋钱罐验签
     * @param $data
     * @param $sign
     * @return int
     * @author luwei
     * @date 2019-03-13 17:37
     */
    public static function verifyPiggyBank($data, $sign){
        $publicKey = dirname(dirname(dirname(__DIR__))) . '/privateKey/publicKey/' . 'piggy_bank.pem';
        $pubKey = file_get_contents($publicKey);
        $res = openssl_get_publickey($pubKey);
        $data = http_build_query($data);
        $result = openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_MD5);
        openssl_free_key($res);
        return $result;
    }



    /**
     * 聚好贷验签
     * @param $data
     * @param $sign
     * @return int
     * @author shenzengguang
     * @date 2019-03-15 17:37
     */
    public static function verifyGatherBank(array $data, $sign){
        $key = config('union_login.union_gather_key');

        $encrypt = md5($data['mid'].$data['phone'].$data['ip'].$key);

        if ($encrypt == $sign){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 聚好贷加密
     * @param $data
     * @param $sign
     * @return string
     * @author shenzengguang
     * @date 2019-03-15 17:37
     */
    public static function encryptGatherBank(array $data){
        $key = config('union_login.union_gather_key');

        $encrypt = md5($data['mid'].$data['phone'].$data['ip'].$key);

        return $encrypt;

    }

    /**
     * 51验签
     * @param $data
     * @param $sign
     * @return int
     * @author shenzengguang
     * @date 2019-03-15 17:37
     */
    public static function WycardVerifyBank(array $data, $sign){
        $appId = config('union_login.appid');
        //$appId = $data['appId'];
        $channelId = config('union_login.channelid');
        //$channelId = $data['channelId'];
        $mobile = $data['enmobile'];
        $mobileMd5 = $data['mobileMd5'];
        $timestamp = $data['timestamp'];
        $wysign = $sign;

        $data = compact(
            'appId',
            'channelId',
            'mobile',
            'mobileMd5',
            'timestamp'
        );

        //拼接数据
        $str = urldecode(http_build_query($data));
        //读取公钥文件
        $publicKey = dirname(dirname(dirname(__DIR__))) . '/privateKey/publicKey/' . '51gnh_public_key.pem';
        if (!file_exists($publicKey)) {
            throw new GeneralException('私钥文件不存在');
        };
        $pubKey = file_get_contents($publicKey);


        //转换为openssl格式密钥
        $res = openssl_get_publickey($pubKey);

        //openssl校验：需要指定签名算法为MD5，默认未SHA1
        $result = openssl_verify($str, base64_decode($wysign), $res, OPENSSL_ALGO_MD5);
        openssl_free_key($res);
        return $result;


    }


    /**
     * 51验签
     * @param $data
     * @param $sign
     * @return int
     * @author shenzengguang
     * @date 2019-03-15 17:37
     */
    public static function WycardVerifyBankregister(array $data, $sign){


        //拼接数据
        $str = urldecode(http_build_query($data));
        //读取公钥文件
        $publicKey = dirname(dirname(dirname(__DIR__))) . '/privateKey/publicKey/' . '51gnh_public_key.pem';
        if (!file_exists($publicKey)) {
            throw new GeneralException('私钥文件不存在');
        };
        $pubKey = file_get_contents($publicKey);


        //转换为openssl格式密钥
        $res = openssl_get_publickey($pubKey);

        //openssl校验：需要指定签名算法为MD5，默认未SHA1
        $result = openssl_verify($str, base64_decode($sign), $res, OPENSSL_ALGO_MD5);
        openssl_free_key($res);
        return $result;


    }



    /**
     * 51签名不能生成签名
     * @param $data
     * @param $sign
     * @return int
     * @author shenzengguang
     * @date 2019-03-15 17:37
     */
    public static function WycardEncryptBank(array $data){
        $appId = config('union_login.appid');
        $channelId = config('union_login.channelid');
        $mobile = substr_replace($data['mobile'], '****', 7, 4);//手机号  掩码后四位
        $mobileMd5 = md5($data['mobile']);
        $timestamp = microtime_float();

        $data = compact(
            'appId',
            'channelId',
            'mobile',
            'mobileMd5',
            'timestamp'
        );

        //拼接数据
        $str = urldecode(http_build_query($data));
        //读取公钥文件
        $publicKey = dirname(dirname(dirname(__DIR__))) . '/privateKey/publicKey/' . '51gnh_public_key.pem';
        $pubKey = file_get_contents($publicKey);
        //转换为openssl格式密钥
        $res = openssl_get_publickey($pubKey);

        //openssl校验：需要指定签名算法为MD5，默认未SHA1
        //$result = openssl_verify($str, base64_decode($wysign), $res, OPENSSL_ALGO_MD5);

        //return $result;


    }




    /**
     * 随机6位数密码
     * @param int $len
     * @param string $format
     * @return string
     * @author luwei
     * @date 2019-03-14 00:23
     */
    public static function randStr($len=6, $format='ALL') {
        switch($format) {
            case 'ALL':
                $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
            case 'CHAR':
                $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
            case 'NUMBER':
                $chars='0123456789'; break;
            default :
                $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                break;
        }
        mt_srand((double)microtime()*1000000*getmypid());
        $password="";
        while(strlen($password)<$len)
            $password.=substr($chars,(mt_rand()%strlen($chars)),1);
        return $password;
    }


    /**
     * 保街生成加密
     * @param $data 加密所需的参数
     * @return string 加密后的sign
     * @author liChunLong
     * @date 2019-03-15 12:40
     */
    public static function baoJieMakeSign($data) {
        $paramUrl = http_build_query($data , '' , '&');
        $sign = md5($paramUrl);
        return $sign ;
    }
    /**
     * 保街身份验证
     * @param $data 加密所需的参数
     * @param $sign 验证签字
     * @return boole
     * @author liChunLong
     * @date 2019-03-15 12:40
     */
    public static function baoJieVerifySign($data,$sign) {
        $paramUrl = http_build_query($data , '' , '&');
        $make_sign = md5($paramUrl);
        return $make_sign == $sign;
    }
}